/* LEDC (LED Controller) fade example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_log.h"
#include "esp_err.h"
#include "rotat_ec11_drv.h"
#include "sdkconfig.h"
#include "driver/gpio.h"

#include "my_seg_screen_drv.h"

const static char *TAG = "display";

DISP_NUM_t disp_num[3];

uint8_t seg1_data = 0x00;
uint8_t seg2_data = 0x00;
uint8_t seg3_data = 0x00;
uint8_t seg4_data = 0x00;
uint8_t seg5_data = 0x00;

QueueHandle_t  g_display_queue         = NULL;

/******************************************/
// 手写延时 函数
/******************************************/
static void I2C_delay(void)
{
	uint8_t t=10;
	while(t--);
}

/*************************************
 * 配置为输出引脚
 ******************************/
static void set_gpio_output_mode(gpio_num_t pin)
{
	gpio_config_t io_conf = {};
	
    io_conf.intr_type = GPIO_INTR_DISABLE; 
    io_conf.mode = GPIO_MODE_OUTPUT_OD;
    io_conf.pin_bit_mask = 1ULL<<pin; 
    io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
    io_conf.pull_up_en = GPIO_PULLDOWN_DISABLE;
    gpio_config(&io_conf);
}

/*************************************
 * 配置为输入引脚， 开启上拉电阻
 ******************************/
static void set_gpio_input_mode(gpio_num_t pin)
{
	gpio_config_t io_conf = {};
	
    io_conf.intr_type = GPIO_INTR_DISABLE; 
    io_conf.mode = GPIO_MODE_INPUT;
    io_conf.pin_bit_mask = 1ULL<<pin; 
    io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
    io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
    gpio_config(&io_conf);
}

/*************************************
 * IIC DATA
 *************************************/ 
static inline void my_soft_data_high(void)
{
	gpio_set_level(SOFT_IIC_DATA_PIN, IO_DATA_HIGH);
}

/*************************************
 * IIC DATA
 *************************************/ 
static inline void my_soft_data_low(void)
{
	gpio_set_level(SOFT_IIC_DATA_PIN, IO_DATA_LOW);
}

/**********************************************
 * IIC CLK
 **********************************************/ 
static inline void my_soft_sck_high(void)
{
	gpio_set_level(SOFT_IIC_SCL_PIN, IO_DATA_HIGH);
}

static inline void my_soft_sck_low(void)
{
	gpio_set_level(SOFT_IIC_SCL_PIN, IO_DATA_LOW);
}

//========================================================================
// 函数: void iic_start (void)
// 描述: 
// 参数: 启始信号
// 返回: none.
//========================================================================
static inline void iic_start(void)
{
	set_gpio_output_mode(SOFT_IIC_DATA_PIN);
	I2C_delay();
	my_soft_data_high();
	I2C_delay();
	my_soft_sck_high();
	I2C_delay();
	my_soft_data_low();
	I2C_delay();
	I2C_delay();
	I2C_delay();
	my_soft_sck_low();
}

//========================================================================
// 函数: void iic_stop (void)
// 描述: 
// 参数: 启始信号
// 返回: none.
//========================================================================
static void iic_stop(void)
{
	set_gpio_output_mode(SOFT_IIC_DATA_PIN);

	I2C_delay();
	my_soft_sck_high();
	I2C_delay();
	my_soft_data_low();
	I2C_delay();
	I2C_delay();
	my_soft_data_high();
	I2C_delay();
	// my_soft_sck_low();
    I2C_delay();
    my_soft_sck_high();
}
//========================================================================
// 函数: void SendData (char dat)
// 描述: 
// 参数: 发送的数据. ****发送8bit数据，从低位开始***
// 返回: none.
//========================================================================
static inline void iic_send_one_byte(uint8_t data)
{
	uint8_t i;
    set_gpio_output_mode(SOFT_IIC_DATA_PIN);

	for(i=0;i<8; i++)
	{
		my_soft_sck_low();//将时钟信号设置为低电平
        //将dat的8位从 从低位开始
		if(data&0x01){
			my_soft_data_high();
        }
		else{
			my_soft_data_low();
        }
		I2C_delay();
		my_soft_sck_high();
	
		data >>=1;
        I2C_delay();
        I2C_delay();
    }
}

//========================================================================
// 函数:  
// 描述:  显示字符，十位 显示字符“0”
// 参数: 
// 返回: none.
//========================================================================
static inline void ge_wei_num0(void)
{
	iic_start();
	iic_send_one_byte(0xc0);	//地址命令设置：初始地址00H  对应A0行
	iic_send_one_byte(0x00);
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc1);	//地址命令设置：初始地址00H 对应A1行
	iic_send_one_byte(0x0e);		
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc2);	//地址命令设置：初始地址00H 对应A2行
	iic_send_one_byte(0x02);		
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc3);	//地址命令设置：初始地址00H 对应A3行
	iic_send_one_byte(0x02);		
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc4);	//地址命令设置：初始地址00H 对应A4行
	iic_send_one_byte(0x02);		
    iic_stop();

   	iic_start();
	iic_send_one_byte(0xc5);	//地址命令设置：初始地址00H 对应A5行
	iic_send_one_byte(0x02);
    iic_stop();
}

//========================================================================
// 函数:  
// 描述:  显示字符，十位 显示字符“0”
// 参数:  对应断码字段“A3, B3, C3, D3,E3, F3”
// 返回: none.
//========================================================================
 void disp_num0(uint8_t blot_icon_flag)
{
	iic_start();
	iic_send_one_byte(0xc0);	//地址命令设置：初始地址00H  对应A0行
	iic_send_one_byte(0x00);
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc1);	//地址命令设置：初始地址00H 对应A1行
	iic_send_one_byte(0x0e);		
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc2);	//地址命令设置：初始地址00H 对应A2行
	iic_send_one_byte(0x12);	
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc3);	//地址命令设置：初始地址00H 对应A3行
	if(blot_icon_flag){
		iic_send_one_byte(0x12);
	}
	else{
		iic_send_one_byte(0x02);
	}		
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc4);	//地址命令设置：初始地址00H 对应A4行
	iic_send_one_byte(0x02);		
    iic_stop();

   	iic_start();
	iic_send_one_byte(0xc5);	//地址命令设置：初始地址00H 对应A5行
	iic_send_one_byte(0x00);
    iic_stop();
}


//========================================================================
// 函数:  
// 描述:  显示字符，十位 显示字符“1”
// 参数: 		 对应断码字段“ B3, C3,""
// 返回: none.
//========================================================================
 void disp_num1(uint8_t blot_icon_flag)
{
	iic_start();
	iic_send_one_byte(0xc0);	//地址命令设置：初始地址00H  对应A0行
	iic_send_one_byte(0x00);
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc1);	//地址命令设置：初始地址00H 对应A1行
	iic_send_one_byte(0x04);		
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc2);	//地址命令设置：初始地址00H 对应A2行
	iic_send_one_byte(0x12);		
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc3);	//地址命令设置：初始地址00H 对应A3行
	if(blot_icon_flag){
		iic_send_one_byte(0x10);
	}
	else{
		iic_send_one_byte(0x00);
	}	
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc4);	//地址命令设置：初始地址00H 对应A4行
	iic_send_one_byte(0x00);		
    iic_stop();

   	iic_start();
	iic_send_one_byte(0xc5);	//地址命令设置：初始地址00H 对应A5行
	iic_send_one_byte(0x00);
    iic_stop();
}

//========================================================================
// 函数:  
// 描述: icon 对应的 段
// 参数: 		 
// 返回: none.
//========================================================================

static void icon_to_seg(ICON_CODE_e icon)
{
	switch(icon){
		case ICON_K1:{
			seg3_data |= 0x10;
			break;
		}
		case ICON_K2:{
			seg2_data |= 0x10;
			break;
		}
		case ICON_B1:{
			seg3_data |= 0x08;
			break;
		}
		case ICON_C1:{
			seg2_data |= 0x08;
			break;
		}
		case ICON_A2:{
			seg2_data |= 0x04;
			break;
		}
		case ICON_B2:{
			seg3_data |= 0x04;
			break;
		}
		case ICON_C2:{
			seg4_data |= 0x08;
			break;
		}
		case ICON_D2:{
			seg4_data |= 0x04;
			break;
		}
		case ICON_E2:{
			seg5_data |= 0x04;
			break;
		}
		case ICON_F2:{
			seg5_data |= 0x08;
			break;
		}
		case ICON_G2:{
			seg5_data |= 0x10;
			break;
		}
		case ICON_A3:{
			seg1_data |= 0x02;
			break;
		}
		case ICON_B3:{
			seg2_data |= 0x02;
			break;
		}
		case ICON_C3:{
			seg1_data |= 0x04;
			break;
		}
		case ICON_D3:{
			seg3_data |= 0x02;
			break;
		}
		case ICON_E3:{
			seg1_data |= 0x08;
			break;
		}
		case ICON_F3:{
			seg4_data |= 0x02;
			break;
		}
		case ICON_G3:{
			seg5_data |= 0x02;
			break;
		}
		default:{
			break;
		}
	}
}

//========================================================================
// 函数:  
// 描述: number 对应的 段
// 参数: 		 
// 返回: none.
//========================================================================

static void number_to_icon(DISP_NUMBER_e num)
{
	
	switch(num){
		// 个位
		case GE_WEI_0:{
			disp_num[0].icon[0] = ICON_A3;
			disp_num[0].icon[1] = ICON_B3;
			disp_num[0].icon[2] = ICON_C3;
			disp_num[0].icon[3] = ICON_D3;
			disp_num[0].icon[4] = ICON_E3;
			disp_num[0].icon[5] = ICON_F3;
			disp_num[0].icon[6] = ICON_NULL;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}

		case GE_WEI_1:{
			disp_num[0].icon[0] = ICON_B3;
			disp_num[0].icon[1] = ICON_C3;
			disp_num[0].icon[2] = ICON_NULL;
			disp_num[0].icon[3] = ICON_NULL;
			disp_num[0].icon[4] = ICON_NULL;
			disp_num[0].icon[5] = ICON_NULL;
			disp_num[0].icon[6] = ICON_NULL;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}
		case GE_WEI_2:{
			disp_num[0].icon[0] = ICON_A3;
			disp_num[0].icon[1] = ICON_B3;
			disp_num[0].icon[2] = ICON_E3;
			disp_num[0].icon[3] = ICON_G3;
			disp_num[0].icon[4] = ICON_D3;
			disp_num[0].icon[5] = ICON_NULL;
			disp_num[0].icon[6] = ICON_NULL;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}
		case GE_WEI_3:{
			disp_num[0].icon[0] = ICON_A3;
			disp_num[0].icon[1] = ICON_B3;
			disp_num[0].icon[2] = ICON_C3;
			disp_num[0].icon[3] = ICON_G3;
			disp_num[0].icon[4] = ICON_D3;
			disp_num[0].icon[5] = ICON_NULL;
			disp_num[0].icon[6] = ICON_NULL;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}
		case GE_WEI_4:{
			disp_num[0].icon[0] = ICON_F3;
			disp_num[0].icon[1] = ICON_B3;
			disp_num[0].icon[2] = ICON_C3;
			disp_num[0].icon[3] = ICON_G3;
			disp_num[0].icon[4] = ICON_NULL;
			disp_num[0].icon[5] = ICON_NULL;
			disp_num[0].icon[6] = ICON_NULL;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}

		case GE_WEI_5:{
			disp_num[0].icon[0] = ICON_F3;
			disp_num[0].icon[1] = ICON_A3;
			disp_num[0].icon[2] = ICON_C3;
			disp_num[0].icon[3] = ICON_G3;
			disp_num[0].icon[4] = ICON_D3;
			disp_num[0].icon[5] = ICON_NULL;
			disp_num[0].icon[6] = ICON_NULL;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}
		case GE_WEI_6:{
			disp_num[0].icon[0] = ICON_F3;
			disp_num[0].icon[1] = ICON_A3;
			disp_num[0].icon[2] = ICON_C3;
			disp_num[0].icon[3] = ICON_G3;
			disp_num[0].icon[4] = ICON_D3;
			disp_num[0].icon[5] = ICON_E3;
			disp_num[0].icon[6] = ICON_NULL;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}
		case GE_WEI_7:{
			disp_num[0].icon[0] = ICON_A3;
			disp_num[0].icon[1] = ICON_B3;
			disp_num[0].icon[2] = ICON_C3;
			disp_num[0].icon[3] = ICON_NULL;
			disp_num[0].icon[4] = ICON_NULL;
			disp_num[0].icon[5] = ICON_NULL;
			disp_num[0].icon[6] = ICON_NULL;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}

		case GE_WEI_8:{
			disp_num[0].icon[0] = ICON_A3;
			disp_num[0].icon[1] = ICON_B3;
			disp_num[0].icon[2] = ICON_C3;
			disp_num[0].icon[3] = ICON_D3;
			disp_num[0].icon[4] = ICON_E3;
			disp_num[0].icon[5] = ICON_F3;
			disp_num[0].icon[6] = ICON_G3;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}
		case GE_WEI_9:{
			disp_num[0].icon[0] = ICON_A3;
			disp_num[0].icon[1] = ICON_B3;
			disp_num[0].icon[2] = ICON_C3;
			disp_num[0].icon[3] = ICON_D3;
			disp_num[0].icon[4] = ICON_G3;
			disp_num[0].icon[5] = ICON_F3;
			disp_num[0].icon[6] = ICON_NULL;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}
		// 十位----------------
		case SHI_WEI_0:{
			disp_num[1].icon[0] = ICON_A2;
			disp_num[1].icon[1] = ICON_B2;
			disp_num[1].icon[2] = ICON_C2;
			disp_num[1].icon[3] = ICON_D2;
			disp_num[1].icon[4] = ICON_E2;
			disp_num[1].icon[5] = ICON_F2;
			disp_num[1].icon[6] = ICON_NULL;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		
		case SHI_WEI_1:{
			disp_num[1].icon[0] = ICON_B2;
			disp_num[1].icon[1] = ICON_C2;
			disp_num[1].icon[2] = ICON_NULL;
			disp_num[1].icon[3] = ICON_NULL;
			disp_num[1].icon[4] = ICON_NULL;
			disp_num[1].icon[5] = ICON_NULL;
			disp_num[1].icon[6] = ICON_NULL;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		case SHI_WEI_2:{
			disp_num[1].icon[0] = ICON_A2;
			disp_num[1].icon[1] = ICON_B2;
			disp_num[1].icon[2] = ICON_E2;
			disp_num[1].icon[3] = ICON_G2;
			disp_num[1].icon[4] = ICON_D2;
			disp_num[1].icon[5] = ICON_NULL;
			disp_num[1].icon[6] = ICON_NULL;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		case SHI_WEI_3:{
			disp_num[1].icon[0] = ICON_A2;
			disp_num[1].icon[1] = ICON_B2;
			disp_num[1].icon[2] = ICON_C2;
			disp_num[1].icon[3] = ICON_G2;
			disp_num[1].icon[4] = ICON_D2;
			disp_num[1].icon[5] = ICON_NULL;
			disp_num[1].icon[6] = ICON_NULL;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		case SHI_WEI_4:{
			disp_num[1].icon[0] = ICON_F2;
			disp_num[1].icon[1] = ICON_B2;
			disp_num[1].icon[2] = ICON_C2;
			disp_num[1].icon[3] = ICON_G2;
			disp_num[1].icon[4] = ICON_NULL;
			disp_num[1].icon[5] = ICON_NULL;
			disp_num[1].icon[6] = ICON_NULL;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		case SHI_WEI_5:{
			disp_num[1].icon[0] = ICON_F2;
			disp_num[1].icon[1] = ICON_A2;
			disp_num[1].icon[2] = ICON_C2;
			disp_num[1].icon[3] = ICON_G2;
			disp_num[1].icon[4] = ICON_D2;
			disp_num[1].icon[5] = ICON_NULL;
			disp_num[1].icon[6] = ICON_NULL;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		case SHI_WEI_6:{
			disp_num[1].icon[0] = ICON_F2;
			disp_num[1].icon[1] = ICON_A2;
			disp_num[1].icon[2] = ICON_C2;
			disp_num[1].icon[3] = ICON_G2;
			disp_num[1].icon[4] = ICON_D2;
			disp_num[1].icon[5] = ICON_E2;
			disp_num[1].icon[6] = ICON_NULL;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		case SHI_WEI_7:{
			disp_num[1].icon[0] = ICON_A2;
			disp_num[1].icon[1] = ICON_B2;
			disp_num[1].icon[2] = ICON_C2;
			disp_num[1].icon[3] = ICON_NULL;
			disp_num[1].icon[4] = ICON_NULL;
			disp_num[1].icon[5] = ICON_NULL;
			disp_num[1].icon[6] = ICON_NULL;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		case SHI_WEI_8:{
			disp_num[1].icon[0] = ICON_A2;
			disp_num[1].icon[1] = ICON_B2;
			disp_num[1].icon[2] = ICON_C2;
			disp_num[1].icon[3] = ICON_D2;
			disp_num[1].icon[4] = ICON_E2;
			disp_num[1].icon[5] = ICON_F2;
			disp_num[1].icon[6] = ICON_G2;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		case SHI_WEI_9:{
			disp_num[1].icon[0] = ICON_A2;
			disp_num[1].icon[1] = ICON_B2;
			disp_num[1].icon[2] = ICON_C2;
			disp_num[1].icon[3] = ICON_D2;
			disp_num[1].icon[4] = ICON_G2;
			disp_num[1].icon[5] = ICON_F2;
			disp_num[1].icon[6] = ICON_NULL;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		//---------百位
		case BAI_WEI_1:{
			disp_num[2].icon[0] = ICON_B1;
			disp_num[2].icon[1] = ICON_C1;
			disp_num[2].icon[2] = ICON_NULL;
			disp_num[2].icon[3] = ICON_NULL;
			disp_num[2].icon[4] = ICON_NULL;
			disp_num[2].icon[5] = ICON_NULL;
			disp_num[2].icon[6] = ICON_NULL;
			disp_num[2].icon[7] = ICON_NULL;
			break;
		}	
		default:{

			break;
		}
	}
}

//========================================================================
// 函数:  
// 描述:  显示字符，十位 显示字符“2”
// 参数: 
// 返回: none.
//========================================================================
void total_seg_disp(void)
{
	iic_start();
    iic_send_one_byte(0xc0);	//地址命令设置：初始地址00H，对应A1行
	iic_send_one_byte(0);		
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc1);	//地址命令设置：初始地址00H，对应A1行
	iic_send_one_byte(seg1_data);		
    iic_stop();


    iic_start();
    iic_send_one_byte(0xc2);	//地址命令设置：初始地址00H 对应A2行
	iic_send_one_byte(seg2_data);		
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc3);	//地址命令设置：初始地址00H  对应A3行
	iic_send_one_byte(seg3_data);		
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc4);	//地址命令设置：初始地址00H 对应A4行
	iic_send_one_byte(seg4_data);		
    iic_stop();

   	iic_start();
	iic_send_one_byte(0xc5);	//地址命令设置：初始地址00H 对应A5行
	iic_send_one_byte(seg5_data);
    iic_stop();
}


//========================================================================
// 函数: void SendData (char dat)
// 描述: 
// 参数: 发送的数据. ****发送8bit数据，从低位开始***
// 返回: none.
//========================================================================
void disp_test(void)
{
    unsigned char i;
	static int disp_cnt;

	seg1_data = 0x00;
	seg2_data = 0x00;
	seg3_data = 0x00;
	seg4_data = 0x00;
	seg5_data = 0x00;
	memset(disp_num, 0 ,3*sizeof(DISP_NUM_t));

	disp_cnt ++;
	
	disp_cnt = disp_cnt%121;

	// 个位
	number_to_icon(disp_cnt%10); 
	for(i= 0; i < 8; i ++)
	{
		if(disp_num[0].icon[i] == ICON_NULL){
			break;
		}
		icon_to_seg(disp_num[0].icon[i]);	
	}
	// 十位
	if(disp_cnt >= 10){
		number_to_icon((disp_cnt/10)%10 + SHI_WEI_0);
		for(i= 0; i < 8; i ++)
		{
			if(disp_num[1].icon[i] == ICON_NULL){
				break;
			}
			icon_to_seg(disp_num[1].icon[i]);	
		}
	}
	// 百位
	if(disp_cnt >= 100){
		number_to_icon(BAI_WEI_1);
		for(i= 0; i < 8; i ++)
		{
			if(disp_num[2].icon[i] == ICON_NULL){
				break;
			}
			icon_to_seg(disp_num[2].icon[i]);	
		}
	}
	// 闪电图标
	if(disp_cnt%2){
		icon_to_seg(ICON_K1);
	}
	// 百分比图标
	icon_to_seg(ICON_K2);
	total_seg_disp();

	// 打开显示
    iic_start();
    iic_send_one_byte(0x8c);	//显示控制：显示开，脉冲宽度设为11/16
    iic_stop();
}

/*========================================================================
 函数: 
// 描述: 
// 参数: data 需要显示的数据
		flick_type ：闪烁类型
		flick_time : 以100ms 为单位，闪烁频率
// 返回: flash_icon 闪电图标
========================================================================*/
void sys_data_disp(uint8_t data, uint8_t flick_type, uint8_t flick_time)
{
    unsigned char i;
	static uint8_t flick_cnt = 0, flick_flag = 0;

	if(data > 188){
		ESP_LOGE(TAG, "disp_number too big");
		return;
	}

	if(flick_type == NO_FLICK){
		flick_cnt = 0;
		flick_flag = 1;
	}
	else{
		flick_cnt ++;
		if(flick_cnt > flick_time){
			flick_cnt = 0;
			flick_flag = (flick_flag == 0) ? 1: 0;
			ESP_LOGI(TAG, "flick_flag :[%d], flick_time:[%d]ms", flick_flag, flick_time *100);
		}
	}

	seg1_data = 0x00;
	seg2_data = 0x00;
	seg3_data = 0x00;
	seg4_data = 0x00;
	seg5_data = 0x00;
	memset(disp_num, 0 ,3*sizeof(DISP_NUM_t));

	// data ++;

	// 个位
	number_to_icon(data%10); 
	if(((flick_type & GE_FLICK) && (flick_flag == 1)) || ((flick_type & GE_FLICK) == 0)){
		for(i= 0; i < 8; i ++){
			if(disp_num[0].icon[i] == ICON_NULL){
				break;
			}
			icon_to_seg(disp_num[0].icon[i]);	
		}
	}
	// 十位
	if(((flick_type & SHI_FLICK) && (flick_flag == 1)) || ((flick_type & SHI_FLICK) == 0)){
		if(data >= 10){
			number_to_icon((data/10)%10 + SHI_WEI_0);
			for(i= 0; i < 8; i ++){
				if(disp_num[1].icon[i] == ICON_NULL){
					break;
				}
				icon_to_seg(disp_num[1].icon[i]);	
			}
		}
	}
	// 百位
	if(((flick_type & BAI_FLICK) && (flick_flag == 1)) || ((flick_type & BAI_FLICK) == 0)){
		if(data >= 100){
			number_to_icon(BAI_WEI_1);
			for(i= 0; i < 8; i ++){
				if(disp_num[2].icon[i] == ICON_NULL){
					break;
				}
				icon_to_seg(disp_num[2].icon[i]);	
			}
		}
	}
	// 闪电图标
	if(((flick_type & ICON1_FLICK) && (flick_flag == 1)) || ((flick_type & ICON1_FLICK) == 0)){
		icon_to_seg(ICON_K1);
	}
	// 百分比图标
	if(((flick_type & ICON2_FLICK) && (flick_flag == 1)) || ((flick_type & ICON2_FLICK) == 0)){
		icon_to_seg(ICON_K2);
	}

	//------刷新断码屏-----
	total_seg_disp();

	// 打开显示
    iic_start();
    iic_send_one_byte(0x8c);	//显示控制：显示开，脉冲宽度设为11/16
    iic_stop();
}

/**************************************************
 * 
 * ntc 任务
 **************************************************/
static void seg_display_task(void *arg)
{
	disp_data_t disp_data;

	disp_data.display_data = 87;
	disp_data.flick_type = ICON1_FLICK;
	disp_data.flick_time = 5;

    for (;;) {

		// if (xQueueReceive(g_display_queue, (void *)&disp_data, portMAX_DELAY)) {
		if (xQueueReceive(g_display_queue, (void *)&disp_data, pdMS_TO_TICKS(100))) {
		
			ESP_LOGI(TAG, "disp_recv, num[%d], type:[%d], time:[%d]ms", disp_data.display_data, disp_data.flick_type, disp_data.flick_time);
		}
		sys_data_disp(disp_data.display_data, disp_data.flick_type, disp_data.flick_time);
	
    }
    vTaskDelete(NULL);
}

/*******************************************
 * IIC CLK ,soft iic 初始化
 *******************************************/ 
void my_seg_screen_init(void)
{
	int ret = 0;

	ESP_LOGI(TAG, "my_seg_screen_init begin =====");
    set_gpio_output_mode(SOFT_IIC_DATA_PIN);
    set_gpio_output_mode(SOFT_IIC_SCL_PIN);

	g_display_queue = xQueueCreate(10, sizeof(disp_data_t));

	if(g_display_queue == NULL){ 
        ESP_LOGE(TAG, "failed to create g_display_queue= %p \n", g_display_queue);
        return;
	}
	else{
		ESP_LOGI(TAG, " creat g_display_queue succ");
	}

    ret = xTaskCreate(seg_display_task, "seg_display", 4096, NULL, 5, NULL);
    if(ret == 0){
        ESP_LOGE(TAG, " creat seg_display_task err!!");
		return;
    }
    else{
        ESP_LOGI(TAG, " creat seg_display_task succ");
    }

	ESP_LOGI(TAG, "my_seg_screen_init succ");
	// 测试，开机显示 66 ，500
	// sys_data_disp(66, GE_FLICK|ICON1_FLICK, 5);

}



